/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lcduilwuit;

/**
 *
 * @author anup.s
 */
import com.sun.lwuit.Component;
import com.sun.lwuit.Image;
import com.sun.lwuit.list.DefaultListCellRenderer;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.Container;
import com.sun.lwuit.layouts.*;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.Command;

public class List extends  Form implements Displayable, Choice/*, SelectionListener*/ {

    private com.sun.lwuit.List list;
    private int listType;
    private MyListRenderer rndr;
    public static final Command SELECT_COMMAND = new Command("");
    private Command selectCommand = SELECT_COMMAND;
    ActionListener commandListener;

    public List(String title, int listType) {
        super(title);
        if (listType != Choice.EXCLUSIVE &&
                listType != Choice.IMPLICIT &&
                listType != Choice.MULTIPLE) {
            throw new IllegalArgumentException("ListType"+listType+" not supported");
        }
        list = new MyList();
        this.listType = listType;
        rndr = new MyListRenderer();
        setListProperty();
        setLayout(Display.boxLayout_Y);
        addComponent(list);
    //Creates a new, empty List, specifying its title and the type of the list.
    }

    public List(String title, int listType, String[] stringElements, Image[] imageElements) {
        super(title);//,listType);
        if (stringElements == null) {
            throw new NullPointerException("stringElements is null");
        }
        if (imageElements != null && imageElements.length != stringElements.length) {
                
            throw new IllegalArgumentException("imageElements and stringElements length is not same");
        }
        if (listType != Choice.EXCLUSIVE &&
                listType != Choice.IMPLICIT &&
                listType != Choice.MULTIPLE) {
            throw new IllegalArgumentException("ListType"+listType+" not supported");
        }
        if (listType != Choice.EXCLUSIVE ||
                listType != Choice.IMPLICIT ||
                listType != Choice.MULTIPLE) {
            throw new IllegalArgumentException("ListType"+listType+" not supported");
        }
        this.listType = listType;
        ListItem[] listItem = new ListItem[stringElements.length];
        for (int i = 0; i < stringElements.length; i++) {
            if (stringElements[i] == null) {
                throw new NullPointerException("stringElements[" + i + "] is null");
            }
            listItem[i] = new ListItem(stringElements[i], imageElements[i]);
        }
        rndr = new MyListRenderer();
        list = new MyList(listItem);
        setListProperty();
        addComponent(list);
    }

    private void setListProperty() {
        list.getStyle().setBgTransparency(0);
        list.setListCellRenderer(rndr);
        list.setOrientation(com.sun.lwuit.List.VERTICAL);
        list.setItemGap(1);
        list.setBorderGap(1);
        Style s = list.getStyle();
        s.setBorder(Border.createLineBorder(1));
        list.setStyle(s);
    }

    public int append(String stringPart, Image imagePart) {
        list.addItem(new ListItem(stringPart, imagePart));
        return list.size() - 1;
    }

    public int getSelectedIndex() {
        return list.getSelectedIndex();
    }

    public void delete(int id) {
        com.sun.lwuit.list.ListModel mdl = list.getModel();
        mdl.removeItem(id);
        list.setModel(mdl);
    }

    public int size() {
        return list.size();
    }

    public String getString(int elementNum) {
        return ((ListItem) list.getModel().getItemAt(elementNum)).getText();
    }

    public void setCommandListener(ActionListener commandListener) {
        this.commandListener = commandListener;
        super.setCommandListener(commandListener);
    }

    private void fireSelectCommand() {
        if (commandListener != null) {
            commandListener.actionPerformed(new com.sun.lwuit.events.ActionEvent(selectCommand));
        }
    }

    public void setSelectCommand(Command cmd) {
        selectCommand = cmd;
    }

    public void getSelectedFlags(boolean[] checked) {

        checked[list.getSelectedIndex()] = true;

    }
    class MyList extends  com.sun.lwuit.List
    {
        long tickTime = System.currentTimeMillis();
        private int lastSelection = -1;
        MyList(java.lang.Object[] items)
        {
          super(items);
        }
        MyList()
        {
            super();
        }
       public void keyReleased(int key) {
            int gameAction = com.sun.lwuit.Display.getInstance().getGameAction(key);
            if (gameAction == com.sun.lwuit.Display.GAME_FIRE) {
                List.this.fireSelectCommand();
            } else {
                super.keyReleased(key);
            }
        }

        public boolean animate() {
            boolean val = super.animate();
            if (hasFocus()) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - tickTime > 100) {
                    if (lastSelection == getSelectedIndex()) {
                        rndr.incrementPosition();
                    } else {
                        lastSelection = getSelectedIndex();
                        rndr.resetPosition();
                    }
                    tickTime = System.currentTimeMillis();
                    return true;
                }
            }
            return val;
        }
    }
    class MyListRenderer extends DefaultListCellRenderer{//implements ListCellRenderer {

        
        private com.sun.lwuit.Label focus = new com.sun.lwuit.Label("");

        public MyListRenderer() {
            focus.getStyle().setBgColor(0x42647F);
            focus.getStyle().setBgTransparency(128);
        }
        private int position;
        public void resetPosition() {
            position = 0;
        }
        public void incrementPosition() {
            position -= 5;
        }
        public Component getListCellRendererComponent(com.sun.lwuit.List list, Object value, int index, boolean isSelected) {

            if (!(value instanceof ListItem)) {
                throw new IllegalArgumentException("Value is not instance of ListItem");
            }
            ListItem item = (ListItem) value;
            setText(item.getText());
            getStyle().setBgTransparency(0);
            if (item.getImg() != null) {
                setIcon(item.getImg());
            }
            if(list.hasFocus() && isSelected) {
                int textWidth = getStyle().getFont().stringWidth(getText());
                if (textWidth > getWidth()) {
                    setShiftText(position);
                }
                setEndsWith3Points(false);
                if (textWidth + position < 0) {
                    position = getWidth();
                }
            }else{
                setShiftText(0);
                setEndsWith3Points(true);
            }

            return this;


        }

        public Component getListFocusComponent(com.sun.lwuit.List list) {
            return focus;
        }
    }

    class ListItem {

        String text;
        Image img;

        public ListItem(String text, Image img) {
            this.text = text;
            this.img = img;
        }

        public Image getImg() {
            return img;
        }

        public String getText() {
            return text;
        }

        public void setImg(Image img) {
            this.img = img;
        }

        public void setText(String text) {
            this.text = text;
        }
    }
}
